
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var increasingBST = function(root) {
  let nodeArr = [];
  function preView(node) {
    if(!node) return;
    nodeArr.push(node);
    preView(node.left);
    preView(node.right);
  }
  preView(root);
  nodeArr.sort((a, b) => a.val - b.val);
  for(let i = 0; i < nodeArr.length; i++) {
    nodeArr[i].left = null;
    nodeArr[i].right = nodeArr[i + 1];
  }
  nodeArr[nodeArr.length - 1].left = null;
  nodeArr[nodeArr.length - 1].right = null;
  return nodeArr[0];
};
